/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.moedik.controller;

import com.moedik.classes.Flight;
import com.moedik.classes.FlightAirline;
import com.moedik.classes.FlightAirport;
import com.moedik.classes.FlightBook;
import com.moedik.classes.FlightClass;
import com.moedik.classes.User;
import com.moedik.model.ConfigModel;
import com.moedik.model.FlightModel;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author LegACy
 */
public class FlightController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ConfigModel configModel = new ConfigModel();
        String companyName = configModel.getCompanyName();
        request.getSession().setAttribute("companyName", companyName);
        request.setAttribute("title", "Shuttle");

        FlightModel flight = new FlightModel();
        if ((request.getParameter("action") == null) || (request.getParameter("action").equals("view"))) {
            if (request.getParameter("get") != null) {
                response.setContentType("text/plain");
                PrintWriter out = response.getWriter();
                if (request.getParameter("get").equals("schedule")) {
                    response.setContentType("text/html;charset=UTF-8");
                    int airportId = Integer.parseInt(request.getParameter("airport"));
                    long date = Long.parseLong(request.getParameter("date"));
                    ArrayList<Flight> schedules = flight.getServices(airportId, date);
                    request.setAttribute("schedules", schedules);
                    request.getRequestDispatcher("flights/list.jsp").forward(request, response);
                }
            } else {
                ArrayList<FlightAirport> airports = flight.getAirport();
                request.setAttribute("airports", airports);
                request.getRequestDispatcher("flights/view.jsp").forward(request, response);
            }
        } else if (request.getParameter("action").equals("confirm_book")) {
            int serviceId = Integer.parseInt(request.getParameter("id"));
            String date = request.getParameter("date");
            String hour = request.getParameter("hour");
            Flight service = flight.getService(serviceId);

            request.setAttribute("service", service);
            request.setAttribute("date", date);
            request.setAttribute("hour", hour);
            request.getRequestDispatcher("flights/confirm_book.jsp").forward(request, response);
        } else if (request.getParameter("action").equals("book")) {
            User user = (User) request.getSession().getAttribute("user");
            int serviceId = Integer.parseInt(request.getParameter("service_id"));
            String date = request.getParameter("date");
            String hour = request.getParameter("hour");
            Flight service = flight.getService(serviceId);

            String bookingCode = flight.book(user.getId(), serviceId, date + ' ' + hour);

            request.setAttribute("user", user);
            request.setAttribute("code", bookingCode);
            request.setAttribute("service", service);
            request.setAttribute("date", date);
            request.setAttribute("hour", hour);

            request.getRequestDispatcher("flights/book.jsp").forward(request, response);
        } else if (request.getParameter("action").equals("manage")) {
            boolean authenticated = (Boolean) request.getSession().getAttribute("authenticated");

            if (!authenticated) {
                response.sendRedirect("./users?action=login");
            } else {
                if (((User) request.getSession().getAttribute("user")).getRoleId() != 2) {
                    request.getSession().setAttribute("flash", "Silahkan login sebagai manager");
                    response.sendRedirect("./users?action=login");
                } else if (request.getParameter("get") != null) {
                    if (request.getParameter("get").equals("schedule")) {
                        response.setContentType("text/html;charset=UTF-8");
                        int airportId = Integer.parseInt(request.getParameter("airport"));
                        long date = Long.parseLong(request.getParameter("date"));
                        ArrayList<Flight> schedules = flight.getServices(airportId, date);
                        request.setAttribute("schedules", schedules);
                        request.getRequestDispatcher("flights/manage_list.jsp").forward(request, response);
                    }
                } else if (request.getParameter("mode") != null) {
                    if (request.getParameter("mode").equals("delete")) {
                        int schedId = Integer.parseInt(request.getParameter("schedule"));
                        flight.deleteService(schedId);
                    } else if (request.getParameter("mode").equals("edit")) {
                        if (request.getMethod().equals("POST")) {
                            int airportFrom = Integer.parseInt(request.getParameter("airportFrom"));
                            int airportTo = Integer.parseInt(request.getParameter("airportTo"));
                            int airlineId = Integer.parseInt(request.getParameter("airline"));
                            int airlineClass = Integer.parseInt(request.getParameter("class"));
                            int schedId = Integer.parseInt(request.getParameter("id"));
                            String hour = request.getParameter("hour");
                            String minute = request.getParameter("minute");
                            int fare = Integer.parseInt(request.getParameter("fare"));
                            String date = request.getParameter("date");
                            flight.updateService(schedId, airportFrom, airportTo, airlineId, airlineClass, fare, date, hour, minute);
                            request.getSession().setAttribute("flash", "Schedule berhasil diedit");
                        }
                        int serviceId = Integer.parseInt(request.getParameter("schedule"));
                        Flight schedule = flight.getService(serviceId);
                        ArrayList<FlightAirport> airports = flight.getAirports();
                        ArrayList<FlightAirline> airlines = flight.getAirlines();
                        ArrayList<FlightClass> classes = flight.getClasses();
                        request.setAttribute("airports", airports);
                        request.setAttribute("airlines", airlines);
                        request.setAttribute("classes", classes);
                        request.setAttribute("schedule", schedule);
                        request.getRequestDispatcher("flights/manage_edit.jsp").forward(request, response);

                    } else if (request.getParameter("mode").equals("add")) {
                        if (request.getMethod().equals("POST")) {
                            int airportFrom = Integer.parseInt(request.getParameter("airportFrom"));
                            int airportTo = Integer.parseInt(request.getParameter("airportTo"));
                            int airlineId = Integer.parseInt(request.getParameter("airline"));
                            int airlineClass = Integer.parseInt(request.getParameter("class"));
                            String hour = request.getParameter("hour");
                            String minute = request.getParameter("minute");
                            int fare = Integer.parseInt(request.getParameter("fare"));
                            String date = request.getParameter("date");
                            flight.addService(airportFrom, airportTo, airlineId, airlineClass, fare, date, hour, minute);
                            request.getSession().setAttribute("flash", "Schedule berhasil ditambahkan");
                        }
                        ArrayList<FlightAirport> airports = flight.getAirports();
                        ArrayList<FlightAirline> airlines = flight.getAirlines();
                        ArrayList<FlightClass> classes = flight.getClasses();
                        request.setAttribute("airports", airports);
                        request.setAttribute("airlines", airlines);
                        request.setAttribute("classes", classes);
                        request.getRequestDispatcher("flights/manage_add.jsp").forward(request, response);
                    }
                } else {
                    ArrayList<FlightAirport> airports = flight.getAirports();
                    request.setAttribute("airports", airports);
                    request.getRequestDispatcher("flights/manage.jsp").forward(request, response);
                }
            }
        } else if (request.getParameter("action").equals("view_book")) {
            boolean authenticated = (Boolean) request.getSession().getAttribute("authenticated");

            if (!authenticated) {
                response.sendRedirect("./users?action=login");
            } else {
                if (((User) request.getSession().getAttribute("user")).getRoleId() != 3) {
                    request.getSession().setAttribute("flash", "Silahkan login sebagai karyawan");
                    response.sendRedirect("./users?action=login");
                } else if (request.getParameter("mode") != null) {
                    if (request.getParameter("mode").equals("delete_book")) {
                        int id = Integer.parseInt(request.getParameter("id"));
                        flight.deleteFlightBook(id);
                        //view booking list
                        ArrayList<FlightBook> flightBooks = flight.getFlightBooks();
                        request.getSession().setAttribute("flash", "Penghapusan berhasil.");
                        request.setAttribute("flightBooks", flightBooks);
                        request.getRequestDispatcher("flights/book_list.jsp").forward(request, response);
                    }
                } else {
                    //view booking list
                    ArrayList<FlightBook> flightBooks = flight.getFlightBooks();
                    request.setAttribute("flightBooks", flightBooks);
                    request.getRequestDispatcher("flights/book_list.jsp").forward(request, response);
                }
            }
        }else if (request.getParameter("action").equals("report")) {
            if (((User) request.getSession().getAttribute("user")) == null) {
                response.sendRedirect("./users?action=login");
            }else if (((User) request.getSession().getAttribute("user")).getRoleId() != 2) {
                request.getSession().setAttribute("flash", "Silahkan login sebagai manager");
                response.sendRedirect("./users?action=login");
            } else if(request.getMethod().equals("POST")){
                int year = Integer.parseInt(request.getParameter("year"));
                ArrayList<Integer> count = flight.getReport(year);
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.YEAR, year);
                request.setAttribute("count", count);
                request.setAttribute("year", year);
                request.getRequestDispatcher("flights/report.jsp").forward(request, response);
            }else {
                ArrayList<Integer> count = flight.getReport();
                Calendar cal = Calendar.getInstance();
                request.setAttribute("count", count);
                request.setAttribute("year", cal.get(Calendar.YEAR));
                request.getRequestDispatcher("flights/report.jsp").forward(request, response);
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
